//
//  ChangeSafeCodeViewController.m
//  JiuJiuShangCheng
//
//  Created by jiujiukeji on 2018/8/21.
//  Copyright © 2018年 jiujiukeji. All rights reserved.
//

#import "ChangeSafeCodeViewController.h"
#import "ChangePWItem.h"

@interface ChangeSafeCodeViewController ()

@property (nonatomic, strong) ChangePWItem *item1;
@property (nonatomic, strong) ChangePWItem *item2;
@property (nonatomic, strong) ChangePWItem *item22;
@property (nonatomic, strong) ChangePWItem *item3;
@property (nonatomic, strong) ChangePWItem *item4;
@property (nonatomic, strong) UIButton *codeBtn;
@property (nonatomic, strong) UIButton *btn1;
@property (nonatomic, strong) UIButton *btn2;

@end

@implementation ChangeSafeCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorFromRGB(0xf5f5f5);
    [self loadBaseTitleLable:@"修改安全密码"];
    
    [self setUpUI];
}
- (void)setUpUI
{
    ChangePWItem *item1 = [[ChangePWItem alloc] init];
    item1.textField.text = @"使用原密码验证"; // 使用已绑手机验证
    item1.textField.enabled = NO;
    [self.view addSubview:item1];
    [item1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(rateWidth(660), rateHeight(90)));
        make.top.equalTo(self.view).offset(rateHeight(30)+SafeAreaTopHeight);
        make.centerX.equalTo(self.view);
    }];
    self.item1 = item1;
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionSelect)];
//    [item1 addGestureRecognizer:tap];
    
    ChangePWItem *item2 = [[ChangePWItem alloc] init];
    item2.textField.placeholder = @"请输入旧密码";
    item2.textField.secureTextEntry = YES;
    [self.view addSubview:item2];
    [item2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(rateWidth(660), rateHeight(90)));
        make.top.equalTo(item1.mas_bottom).offset(rateHeight(30));
        make.centerX.equalTo(self.view);
    }];
    self.item2 = item2;
    
    ChangePWItem *item22 = [[ChangePWItem alloc] init];
    item22.textField.placeholder = @"请输入验证码";
    [self.view addSubview:item22];
    [item22 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(rateWidth(320), rateHeight(90)));
        make.top.equalTo(item1.mas_bottom).offset(rateHeight(30));
        make.left.equalTo(self.item1);
    }];
    self.item22 = item22;
    self.item22.hidden = YES;
    
    self.codeBtn = [UIButton buttonWithTitle:@"获取验证码" fontSize:16 titleColor:UIColorFromRGB(0x333333) background:globalGray cornerRadius:3];
    [self.view addSubview:self.codeBtn];
    [self.codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(rateWidth(320), rateHeight(90)));
        make.top.equalTo(item1.mas_bottom).offset(rateHeight(30));
        make.right.equalTo(self.item1);
    }];
    self.codeBtn.hidden = YES;
    
    ChangePWItem *item3 = [[ChangePWItem alloc] init];
    item3.textField.placeholder = @"请输入新密码";
    item3.textField.secureTextEntry = YES;
    [self.view addSubview:item3];
    [item3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(rateWidth(660), rateHeight(90)));
        make.top.equalTo(item2.mas_bottom).offset(rateHeight(30));
        make.centerX.equalTo(self.view);
    }];
    self.item3 = item3;
    
    ChangePWItem *item4 = [[ChangePWItem alloc] init];
    item4.textField.placeholder = @"请再次输入新密码";
    item4.textField.secureTextEntry = YES;
    [self.view addSubview:item4];
    [item4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(rateWidth(660), rateHeight(90)));
        make.top.equalTo(item3.mas_bottom).offset(rateHeight(30));
        make.centerX.equalTo(self.view);
    }];
    self.item4 = item4;
    
    UIButton *btn = [UIButton buttonWithTitle:@"提交" fontSize:16 titleColor:UIColorWhite background:navBarColor cornerRadius:3];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(item4.mas_bottom).offset(rateHeight(40));
        make.centerX.equalTo(self.view);
        make.size.mas_offset(CGSizeMake(rateWidth(660), rateHeight(90)));
    }];
    [btn addTarget:self action:@selector(settingSafeCode) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIButton *btn1 = [UIButton buttonWithTitle:@"使用原密码验证" fontSize:15 titleColor:UIColorFromRGB(0x333333) background:UIColorWhite cornerRadius:0];
    btn1.layer.borderColor = globalGray.CGColor;
    btn1.layer.borderWidth = 0.5;
    [self.view addSubview:btn1];
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(item1.mas_bottom);
        make.centerX.equalTo(self.view);
        make.size.mas_offset(CGSizeMake(rateWidth(660), rateHeight(90)));
    }];
    self.btn1 = btn1;
    btn1.tag = 1;
    
    UIButton *btn2 = [UIButton buttonWithTitle:@"使用已绑手机验证" fontSize:15 titleColor:UIColorFromRGB(0x333333) background:UIColorWhite cornerRadius:0];
    btn2.layer.borderColor = globalGray.CGColor;
    btn2.layer.borderWidth = 0.5;
    [self.view addSubview:btn2];
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btn1.mas_bottom);
        make.centerX.equalTo(self.view);
        make.size.mas_offset(CGSizeMake(rateWidth(660), rateHeight(90)));
    }];
    self.btn2 = btn2;
    btn2.tag = 2;
    [btn1 addTarget:self action:@selector(selectType:) forControlEvents:(UIControlEventTouchUpInside)];
    [btn2 addTarget:self action:@selector(selectType:) forControlEvents:(UIControlEventTouchUpInside)];
    self.btn1.hidden = YES;
    self.btn2.hidden = YES;
    
}
- (void)actionSelect
{
    self.btn1.hidden = NO;
    self.btn2.hidden = NO;
}
- (void)selectType:(UIButton *)btn
{
    if (btn.tag == 1) {
        self.item1.textField.text = @"使用原密码验证";
        self.item2.hidden = NO;
        self.item22.hidden = YES;
        self.codeBtn.hidden = YES;
    } else if (btn.tag == 2) {
        self.item1.textField.text = @"使用已绑手机验证";
        self.item2.hidden = YES;
        self.item22.hidden = NO;
        self.codeBtn.hidden = NO;
    }
    self.btn1.hidden = YES;
    self.btn2.hidden = YES;
}
- (void)settingSafeCode
{
    NSString *code = [NSString stringWithFormat:@"%@%@",safeCode,[GetTimestamp getNowTimeTimestamp]];
    [MBProgressHUD showMessage:nil toView:self.view];
    [TFNetworking postWithURLString:[NSString stringWithFormat:@"%@/api/json/member/safe.ashx?action=paymentpass&companyid=%@&code=%@&timestamp=%@",kServerURL,companyId,[MD5Encrypt  MD5ForLower32Bate:code],[GetTimestamp getNowTimeTimestamp]] parameters:@{@"oldpass":self.item2.textField.text,@"newpass":self.item3.textField.text,@"confirmpass":self.item4.textField.text} success:^(id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        if (responseObject) {
            if ([responseObject[@"status"] intValue] == 1) {
                [self.navigationController popViewControllerAnimated:YES];
                [MBProgressHUD showSuccess:responseObject[@"returnMsg"]];
            } else {
                [TFHUD addRemindHUD:self.view message:responseObject[@"returnMsg"]];
            }
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
